package com.hhf.feiojapiservice.client;

import com.hhf.feiojapiservice.config.DefaultFeignConfig;
import com.hhf.feiojapiservice.fallback.UserClientFallbackFactory;
import com.hhf.feiojcommonservice.common.ErrorCode;
import com.hhf.feiojcommonservice.constant.UserConstant;
import com.hhf.feiojcommonservice.exception.BusinessException;
import com.hhf.feiojcommonservice.model.entity.User;
import com.hhf.feiojcommonservice.model.enums.RoleEnum;
import com.hhf.feiojcommonservice.model.vo.UserVO;
import com.hhf.feiojcommonservice.utils.UserIdContext;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @Author hhf
 * @Date 2024/11/3
 * @Description
 */
@FeignClient(name = "feioj-user-service", path = "/api/user/inner",fallbackFactory = UserClientFallbackFactory.class,configuration = DefaultFeignConfig.class)
public interface UserFeignClient {

    /**
     * 是否为管理员
     *
     * @param user
     * @return
     */
    default boolean isAdmin(User user){
        return user != null && RoleEnum.ADMIN.getValue().equals(user.getRole());
    }


    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    User getLoginUser(HttpServletRequest request);

    @GetMapping("/get/current")
    User getCurrentUser();

    /**
     * 根据ids查询用户
     * @param idList
     * @return
     */
    @GetMapping("/get/ids")
    List<User> listByIds(@RequestParam("idList")Collection<Long> idList);


    /**
     * 获取脱敏的用户信息
     *
     * @param user
     * @return
     */
    default UserVO getUserVO(User user){
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }


}
